package com.example.springboot_template.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_template.Service.UserService;
import com.example.springboot_template.annotation.ParamCheckNUll;
import com.example.springboot_template.common.BaseResponse;
import com.example.springboot_template.common.ResultUtils;
import com.example.springboot_template.mapper.UserMapper;
import com.example.springboot_template.model.domain.DTO.userDTO;
import com.example.springboot_template.model.domain.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/user")
public class userController {

    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ParamCheckNUll
    public boolean userRegister(@RequestBody userDTO user) {
        return userService.saveUser(user);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    //简化步骤，减少校验  只为实现demo
    @PostMapping("/login")
    @ParamCheckNUll
    public BaseResponse<User> userLogin(@RequestBody userDTO user) {
        String userAccount = user.getUseraccount();
        String userPassword = user.getUserpassword();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount", userAccount);
        queryWrapper.eq("userPassword", userPassword);
        User loginUser = userMapper.selectOne(queryWrapper);
        StpUtil.login(loginUser.getId());
        return ResultUtils.success(loginUser);
    }

    /**
     * 查询登录状态
     * @return
     */
    @GetMapping("isLogin")
    public BaseResponse<Boolean> isLogin() {
        return ResultUtils.success(StpUtil.isLogin());
    }

    /**
     * 查询 Token 信息
     * @return
     */
    @GetMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    /**
     * 获取当前账号所拥有的权限集合
     * @return
     */
    @GetMapping("/permission")
    public SaResult getPermissionList() {
        return SaResult.data(StpUtil.getPermissionList());
    }
}
